package com.ruoyi.project.module.lyx;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.project.domain.YqgConsumptionDetails;
import com.ruoyi.project.module.lyx.lei.Chargeback;
import com.ruoyi.project.service.IYqgBarBillsService;
import com.ruoyi.project.service.IYqgConsumptionDetailsService;
import com.ruoyi.project.service.IYqgHandsService;
import com.ruoyi.project.service.IYqgInformationRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 消费详细Controller
 *
 * @author dr
 * @date 2023-06-14
 */
@RestController
@RequestMapping("/lyx/chargeback")
/**这里是api里的module下lyx里的chargeback.js文件*/

public class LyxYqgChargebackController extends BaseController {

    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgHandsService yqgHandsService;
    @Autowired
    private IYqgInformationRoomService yqgInformationRoomService;

    /**
     * 查询消费详细列表
     */

    //@RequiresPermissions("project:details:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgConsumptionDetails yqgConsumptionDetails) {
//                startPage();
//        long startTime = Long.parseLong(yqgConsumptionDetails.getNote1()) + 9 * 60 * 60 * 1000;获取起始时间
//        long endTime = Long.parseLong(yqgConsumptionDetails.getNote2()) + 9 * 60 * 60 * 1000;获取结束时间
//        yqgConsumptionDetails.setNote1(null);清空起始时间
//        yqgConsumptionDetails.setNote2(null);清空结束时间
//        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);  <>里面是类，list是表，此处查询的是消费详细表
//        List<YqgConsumptionDetails> consumptionDetailsList = new ArrayList<>();查询到的结果传到new里
//        for (YqgConsumptionDetails consumptionDetails : list) {对消费详细表做一个循环
//            long time = consumptionDetails.getChargebackTime().getTime();获取表内的退单时间
//            if (time >= startTime && time <= endTime){判断已获取的退单时间是否在查询的开始时间-结束时间内
//                consumptionDetailsList.add(consumptionDetails);如果是则把这条数据添加到new消费详细表里
//            }
//        }
        return getDataTable(handleInfo(yqgConsumptionDetails));    //handleInfo是随意起的方法名字，此处是指将最后查询完的结果传回消费详细表
    }

    public List<Chargeback> handleInfo(YqgConsumptionDetails yqgConsumptionDetails) {      //<内部的Chargeback>是定义的类，是module/lyx/lei下chargeback.java里面定义的
        long startTime = Long.parseLong(yqgConsumptionDetails.getNote1()) + 9 * 60 * 60 * 1000;
        long endTime = Long.parseLong(yqgConsumptionDetails.getNote2()) + 9 * 60 * 60 * 1000;
        yqgConsumptionDetails.setNote1(null);
        yqgConsumptionDetails.setNote2(null);
        if (yqgConsumptionDetails.getDepartment().equals("7")) {     //此处意思是如果部门那里选中了代号为7的总店，则显示时间范围内的全部数据
            yqgConsumptionDetails.setDepartment(null);
        }
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        List<Chargeback> chargebackList = new ArrayList<>();
        for (YqgConsumptionDetails consumptionDetails : list) {
            long time = consumptionDetails.getChargebackTime().getTime();
            if (time >= startTime && time <= endTime) {
                Chargeback chargeback = new Chargeback();     //搜索到的符合条件的数据
                chargeback.setHandBrand(consumptionDetails.getHandBrand());   //此处要set表格内所有数据
                chargeback.setDepartment(consumptionDetails.getDepartment());
                chargeback.setEntryName(consumptionDetails.getEntryName());
                chargeback.setUnitPrice(consumptionDetails.getUnitPrice());
                chargeback.setQuantity(consumptionDetails.getQuantity());
                chargeback.setTotalAmount(consumptionDetails.getTotalAmount());
                chargeback.setRecordingTime(consumptionDetails.getRecordingTime());
                chargeback.setChargebackProcessorName(consumptionDetails.getChargebackProcessorName());
                chargeback.setChargebackTime(consumptionDetails.getChargebackTime());
                chargeback.setReasonForChargeback(consumptionDetails.getReasonForChargeback());
                chargebackList.add(chargeback);    //将符合条件的数据添加到退单列表
            }
        }

        return chargebackList;     //返回退单列表
    }

    /**
     * 导出消费详细列表
     */
//    @RequiresPermissions("project:details:export")
    @Log(title = "导出", businessType = BusinessType.EXPORT)     //就是一个日志，title是导出时表的名字
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails) {
        List<Chargeback> list = handleInfo(yqgConsumptionDetails);
        //计算合计
        int Amount = 0;
        int TotalAmount = 0;
        Chargeback charge = new Chargeback();
        for (Chargeback chargeback : list) {
            Amount = Amount + Integer.valueOf(chargeback.getQuantity());
            TotalAmount = TotalAmount + Integer.valueOf(chargeback.getTotalAmount());
        }
        charge.setHandBrand("合计");
        charge.setQuantity(String.valueOf(Amount));
        charge.setTotalAmount(String.valueOf(TotalAmount));
        list.add(charge);
        ExcelUtil<Chargeback> util = new ExcelUtil<Chargeback>(Chargeback.class);
        util.exportExcel(response, list, "消费详细数据");
    }
}
